// 以下为官方示例
$().ready(function() {
	validateRule();
	$("#sex").val($("#sexHidden").val());
	$("#type").val($("#typeHidden").val());
	$("#schoolLevel").val($("#schoolLevelType").val());
	$("#state").val($("#stateType").val());
	$("#school_education").val($("#education").val())
	// $("#signupForm").validate();

	if( !$("#img").val() ){
		$(".closeBtn").html('');
	}

	let str = '';

	AjaxGet('/sys/user/selectUserById',{id:$("#id").val()}).then(function (res){
		let user = res.data.user , roles = res.data.role;
		let srr = '';
		roles?.forEach(function (v){

			srr += `
					<label class="checkbox-inline">			
						<input name="roleIds" type="checkbox" value="${v.id}"  ${v.roleSign == 'true'? 'checked':''}>${v.roleName}
					</label>		
				`;

		});

		str = `
				<input type="hidden" id="types" value="${user.userType}">
					<div class="form-group">
						<label class="col-sm-3 control-label">姓名：</label>
						<div class="col-sm-8">
							<input id="name" name="name" class="form-control" type="text"
								   value="${user.name ?? ''}">
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-3 control-label">用户名：</label>
						<div class="col-sm-8">
							<input id="username" name="username" class="form-control"
								   type="text" value="${user.username ?? ''}" readonly>
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-3 control-label">手机号码：</label>
						<div class="col-sm-8">
							<input id="mobile" name="mobile" class="form-control"
								   type="number" value="${user.mobile ?? ''}" readonly>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label">身份证号码：</label>
						<div class="col-sm-8">
							<input id="idNumber" name="idNumber" class="form-control"
								   type="text" value="${user.idNumber ?? ''}">
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label">E-mail：</label>
						<div class="col-sm-8">
							<input id="email" name="email" class="form-control" type="email"
								   value="${user.email ?? ''}">
						</div>
					</div>
	
					<div class="form-group">
						<label class="col-sm-3 control-label">性别：</label>
						<div class="col-sm-8">
							<select id="sex" name="sex" class="form-control">
								<option value="">请选择</option>
								<option value="0">未知</option>
								<option value="1">男</option>
								<option value="2">女</option>
							</select>
						</div>
					</div>
					
					<div class="form-group">
						<label class="col-sm-3 control-label">审核状态：</label>
						<div class="col-sm-8">
							<input id="stateType" value="${user.state}" class="form-control"
								   type="hidden">
							<select id="state" name="state" class="form-control">
								<option value="">请选择</option>
								<option value="0">审核中</option>
								<option value="1">通过</option>
								<option value="2">未通过</option>
								<option value="3">未完善</option>
							</select>
						</div>
					</div>

					<div class="form-group">
						<label class="col-sm-3 control-label">状态:</label>
						<div class="col-sm-8">
							<label class="radio-inline"> <input type="radio" name="status" value="1" ${user.status == 1 ? 'checked':''}/>
								正常
							</label> 
							<label class="radio-inline"> <input type="radio" name="status" value="0" ${user.status == 0 ? 'checked':''}/>
								禁用
							</label>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label">角色</label>
						<div class="col-sm-8">
							${srr}
						</div>
					</div>
		`;

		$("#ob").html(str);

		$("#sex").val(user.sex);
		$("#state").val(user.state);

	}).catch(function (err) {
		console.log(err);
	})


});

var type = $("#types").val();
if( type == 0 ){
	$(".student").show();
	$(".teacher").hide();
}else if( type == 1 || type == 2) {
	$(".student").hide();
	$(".teacher").show();
}

$("#userType").val(type)
$("#userType").change(function (){
	if( $(this).val() == 0 ){
		$(".student").show();
		$(".teacher").hide();
	}else if( $(this).val() == 1 || $(this).val() == 2 ) {
		$(".student").hide();
		$(".teacher").show();
	}
})

function update() {

	// $("#roleIds").val(getCheckedRoles());

	$.ajax({
		cache : true,
		type : "POST",
		url : "/sys/user/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg(data.msg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.msg);
			}

		}
	});
}
function getCheckedRoles() {
	var adIds = "";
	$("input:checkbox[name=roleIds]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function setCheckedRoles() {
	var roleIds = $("#roleIds").val();
	alert(roleIds);
	var adIds = "";
	$("input:checkbox[name=roleIds]:checked").each(function(i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2
			},
			password : {
				required : true,
				minlength : 6
			},
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			email : {
				// required : true,
				email : true
			},
			type : {
				required : true
			},
			topic : {
				required : "#newsletter:checked",
				minlength : 2
			},
			agree : "required"
		},
		messages : {

			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名必须两个字符以上"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码必须6个字符以上"
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			}
		}
	})
}
var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"/sys/dept/treeView"
	})
}
function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}

$(".iconclose").click(function (){
	$('#upImg').attr('src','');
	$(".closeBtn").hide();
	$("#img").val('');
})

layui.use(['upload','laydate'], function () {
	var upload = layui.upload, laydate = layui.laydate;

	laydate.render({
		elem: '#timeOfEnrollment'
		,type: 'month'
	});

	upload.render({
		elem: '#btnAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'image/*', //只筛选图片
		multiple:true,
		number:5,
		size:10240,
		done: function (r) {


			$(".closeBtn").html(`
				<img id="upImg" src="${r.data}" alt="" width="100" height="100"><span class="iconclose"><img src="../../../img/close.png"></span>
			`);

			$("#img").val(r.data);

			$(".iconclose").click(function (){
				$(".closeBtn").html('');
				$("#img").val('');
			})

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});